from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
from complaints.models import Spit
from complaints.serializer import UserSerializer, SpitSerializer


# 吐槽点赞
# 吐槽详细信息
class SpitInfoView(APIView):
    def get(self, request, pk):
        spit = Spit.objects.get(id=pk)
        serializer = UserSerializer(spit)
        return Response(serializer.data)


class Spitcollect(APIView):
    # 吐槽列表
    def get(self, request):
        spit = Spit.objects.filter(parent=None)
        serilalizer = UserSerializer(spit, many=True)
        return Response(serilalizer.data)

    # 吐槽发布和评论
    def post(self, request):
        # 发布吐槽不需要用户
        content = request.data
        parent = request.data.get("parent")

        user = request.user

        if parent is None:
            if content:
                serializer = SpitSerializer(data=content)
                # 验证没问题调用
                if serializer.is_valid(raise_exception=True):
                    serializer.save()
                    return Response(serializer.data)
                else:
                    return Response({"mag": "保存失败"})

        else:
            if user.is_authenticated:
                data = {
                    'parent_id': int(parent),
                    'user_id': user.id,
                    'content': content['content']
                }

                serializer = SpitSerializer(data=data)
                if serializer.is_valid(raise_exception=True):
                    serializer.save()
                    return Response(serializer.data)

                return Response({"mag": "保存失败"})


class CommentListAPIView(ListAPIView):
    queryset = Spit.objects.all()
    serializer_class = SpitSerializer

    def get(self, request, *args, **kwargs):
        id = kwargs['pk']
        try:
            spit = Spit.objects.filter(parent_id=id)

        except Spit.DoesNotExist:
            return Response({'error': 'error'})
        serializer = UserSerializer(spit, many=True)

        return Response(serializer.data)


# 　点赞
class SpitupdatethumbupView(APIView):
    def put(self, request, id):
        # 修改Spit表的数据
        spit = Spit.objects.get(id=id)
        # print(spit.thumbup)
        # print('----')

        if spit.hasthumbup == False:
            spit.thumbup += 1
            spit.hasthumbup = True
            spit.save()
            # print(spit.thumbup)
            return Response({'message': 'ok', 'success': True})
        else:
            if spit.thumbup >= 1:
                spit.thumbup -= 1
                spit.hasthumbup = False
                spit.save()
                # print(spit.thumbup)

                return Response({'message': 'ok', 'success': True})


# 吐槽收藏
class SpitcollectView(APIView):
    def put(self, request, pk):
        spit = Spit.objects.get(id=pk)
        if spit.collected is False:
            spit.collected = True
        else:
            spit.collected = False
        spit.save()

        return Response({'message': 'ok', 'success': True})
